package com.gongsir.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gongsir.entity.Movie;
import com.gongsir.entity.MovieExample;
import com.gongsir.mapper.MovieMapper;
import com.gongsir.service.MovieService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 码之泪殇
 * @date 2020/8/24 10:37
 * 到达胜利之前，无法回头
 */
@Service("movieService")
public class MovieServiceImpl implements MovieService {

    @Resource
    private MovieMapper movieMapper;

    @Override
    public Movie getMovieById(int id) {
        return movieMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo<Movie> getAllMovie(String keywords, int page, int size, String orderBy) {
        MovieExample example = new MovieExample();
        MovieExample.Criteria criteria = example.createCriteria();
        if (null == keywords || "".equals(keywords)) {
            example = null;
        } else {
            criteria.andNameLike("%"+keywords+"%");
            example.or(example.createCriteria().andActionLike("%"+keywords+"%"));
        }
        PageHelper.startPage(page,size,orderBy);
        List<Movie> movies = movieMapper.selectByExample(example);
        return new PageInfo<>(movies);
    }

    @Override
    public int deleteMovieById(int id) {
        return movieMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int updateMovie(Movie movie) {
        return movieMapper.updateByPrimaryKeySelective(movie);
    }

    @Override
    public int saveMovie(Movie movie) {
        return movieMapper.insert(movie);
    }
}
